//
//  SpecialViewController.m
//  LimitFree
//
//  Created by Hailong.wang on 15/8/26.
//  Copyright (c) 2015年 Hailong.wang. All rights reserved.
//

#import "SpecialViewController.h"
#import "LeftViewController.h"
#import "LoadingView.h"
#import "Subject.h"
#import "AppListViewController.h"
@interface SpecialViewController (){
    //分类的ID
    NSString* _categoreID;
    //货币类型
    NSString* _number;
    //分类页面
    LeftViewController *_lvc;
    NSMutableArray *_dataArr;
    
  
}

@end

@implementation SpecialViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //注册SubjectCell
   // [self.tableView registerNib:[UINib nibWithNibName:@"Subject" bundle:nil] forCellReuseIdentifier:@"SubjectCell"];
        //[LoadingView showLoadingView:self.view];
    self.title=@"专题";
    
    // Do any additional setup after loading the view.
}
-(void)initData{
    _dataArr=[[NSMutableArray alloc]init];
    [LoadingView showLoadingView:self.view];
    _number=@"10";
    //_categoreID=@"0";
    //将页面设置为第一页
   // self.page=1;
    //去拿网络数据
    [self getNetData];
}
-(void)createView{
    [super createView];
    
    _lvc = [LeftViewController shareSingleton];
}
//将分类展示的行为放在子视图控制器
-(void)toShowAppClassify{
    [_lvc showView:^(NSString* category){
        //NSLog(@"%@",category);
        
        //666 通过id 分类
        //_categoreID=category;
        [self resetParame];
        [self getNetData];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//重置参数
-(void)resetParame{
    //将参数默认改为1，只拿第一页数据
    //self.page=1;
    
}
//获取网络数据
-(void)getNetData{
    //__weak typeof (self) weakSelf=self;
    [NetRequestManager GET:kSubjectUrl parame:[NSDictionary dictionaryWithObjectsAndKeys:_number,@"number",[NSString stringWithFormat:@"%ld",self.page] ,@"page",nil] succes:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"%@",operation.request.URL);
        
        //weakSelf.dataTwo=[[ThemMedol alloc]initWithData:responseObject error:nil];
        
        //NSLog(@"temp=%@",weakSelf.dataTwo);
        
      NSArray* arry=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        //NSLog(@"%@",arry);
        for (NSDictionary *dic in arry) {
           
           
            ThemMedol* them=[[ThemMedol alloc]init];
             them.title=[dic objectForKey:@"title"];
             them.desc_img=[dic objectForKey:@"img"];
            NSArray* applications=[dic objectForKey:@"applications"];
            for (NSDictionary *dict in applications) {
                them.name=[dict objectForKey:@"name"];
                them.downloads=[dict objectForKey:@"downloads"];
                them.iconUrl=[dict objectForKey:@"iconUrl"];
                them.ratingOverall=[dict objectForKey:@"ratingOverall"];
                [_dataArr addObject:them];
                NSLog(@"%@",them);
            }
        }

        //刷新页面
      //  [weakSelf refreshView];
        
  
        [LoadingView hideLoadingView];
//        
        // NSLog(@"%@ %@",operation.request ,responseObject);
    } failed:^(AFHTTPRequestOperation *operation, NSError *error) {
    }];
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellName = @"SubjectCell";
    //复用队列获取 空闲的cell 如果没有 那么 创建新的
    Subject *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
    if (cell==nil) {
        cell=[[[NSBundle mainBundle]loadNibNamed:@"Subject" owner:self options:nil]lastObject];
    }
    ThemMedol *model = [_dataArr objectAtIndex:indexPath.row];
    cell.nameLabel.text=model.name;
    [cell.iconImageView sd_setImageWithURL:[NSURL URLWithString:model.desc_img]];
    cell.applicationname.text=model.applicationId;
    cell.down.text=model.downloads;
    [cell.imgImageView sd_setImageWithURL:[NSURL URLWithString:model.iconUrl]];
    
    return cell;
}
@end
